

cc.Class({
    extends: cc.Component,

    properties: {
        progressBar :{
            default:null,
            type : cc.Node,
        },
        progressLabel :{
            default:null,
            type :cc.Label,
        }
    },
    // 破坏所需的总时间
    totalTime: 0,
    // 已经过去的时间
    elapsedTime: 0,

    diaState:0,

    diaId :0,

    number:0, 



    
    getParams() {
        if (cc.vv.userMgr.depth == 0) {
            let diamondMap = new Map()
            diamondMap.set("D",0) 
            diamondMap.set("E",1) 
            diamondMap.set("F",2) 
            diamondMap.set("G",3) 
            var diamondDepthAdd = diamondMap.get(cc.vv.currentDiamond.name[0])
        }else {
            let diamondMap = new Map()
            diamondMap.set("A",0) 
            diamondMap.set("B",1) 
            diamondMap.set("C",2) 
            diamondMap.set("D",3) 
            diamondMap.set("E",4) 
            diamondMap.set("F",5) 
            diamondMap.set("G",6) 
            var diamondDepthAdd = diamondMap.get(cc.vv.currentDiamond.name[0])
            return params = {
                user_id : cc.vv.userMgr.userid,
                depth : cc.vv.userMgr.depth + diamondDepthAdd,
                position : cc.vv.currentDiamond.name[1],
            }
        }
    },

    // onLoad() {
    //     this.proBar = this.progressBar.getComponent(cc.ProgressBar);
    //     if (!this.proBar) {
    //         console.error("ProgressBar component not found on progressBar node.");
    //     }
    // },
    
    dig() {
        var params = this.getParams()
        this.proBar = this.progressBar.getComponent(cc.ProgressBar); // 得到组件
        cc.log(this.progressBar)
        cc.log(this.proBar)
        cc.vv.http.sendRequest("getOneDiamondInfo", params, function (data) {
            var d = data.data.diamond.durability;
            var d_r = cc.vv.userMgr.dig_rate;
            // 计算总时间
            this.totalTime = d / d_r;
            // 重置已经过去的时间
            this.elapsedTime = 0;

            diaState = data.data.diamond.diamond_state
        }.bind(this), cc.vv.url.diamondServer); // 注意使用bind(this)来确保this的正确指向
    },
    start () {

    },
    sendResourceUpdateToServer() {
        var res = cc.vv.resourceMgr;
        var params = {
            userid: cc.vv.userMgr.userid,
            
            diamond: res.diamond.nowCount,
            gold: res.gold.nowCount,
            stone: res.stone.nowCount,
            iron: res.iron.nowCount,
            silverCoin: res.silverCoin.nowCount,
            goldCoin: res.goldCoin.nowCount
            
        };
        
        cc.vv.http.sendRequest("updateResourceInfo", params, function(data) {
            if (data.code !== 200) {
                cc.log("更新资源失败！:", data.msg);
            } 
                
        }, cc.vv.url.resourceServer);
    },

    updateDiamondToServer() {
        var params =  {
            diamond_id : diaId,
            diamond_state : diaState,
            number : number,
        }
        cc.vv.http.sendRequest("updateDiamondInfoAfterDig", params, function(data) {
            if (data.code !== 200) {
                cc.log("更新资源失败！:", data.msg);
            } 
                
        }, cc.vv.url.diamondServer);
    },
    update(dt) {
        
        // 如果总时间大于0，说明已经开始挖掘
        if (this.totalTime > 0) {
            // 增加已经过去的时间
            this.elapsedTime += dt;
            // 计算进度条的百分比
            var progress = Math.min(this.elapsedTime / this.totalTime, 1);
            // 更新进度条
            this.proBar.progress = progress;
            // 更新标签显示
            this.progressLabel.string = (progress * 100).toFixed(0) + '%';

            // 如果进度达到100%，可以在这里处理完成挖掘的逻辑
            if (progress >= 1) {
                switch(diaState) {
                    case 0  :

                    case 1 :
                        cc.vv.resourceMgr.iron.nowCount += number
                    case 2 :
                        cc.vv.resourceMgr.stone.nowCount += number
                    case 3 :
                        cc.vv.resourceMgr.gold.nowCount += number 
                    case 4 :
                        cc.vv.resourceMgr.diamond.nowCount += number 
                    default:
                        cc.log("something error")
                }
                this.sendResourceUpdateToServer()
                this.updateDiamondToServer()
            }
        }
    },
});
